﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("AjaxSnippets");

AjaxSnippets.ValueExtractors = function() { };

AjaxSnippets.ValueExtractors.prototype = {
    fromAny: function(id) {
        return id;
    },
    fromTextBox: function(id) {
        return $get(id).value;
    },
    fromDropDown: function(id) {
        var el = $get(id);
        return el.options[el.selectedIndex].value;
    },
    fromRadioList: function(id) {
        var i, els = $get(id).getElementsByTagName('input');
        for (i = 0; i < els.length; i++) {
            if (els[i].checked) { return els[i].value; }
        }
        return null;
    },
    fromCheckList: function(id) {
        var i, els = $get(id).getElementsByTagName('input'), vals = new Array();
        for (i = 0; i < els.length; i++) {
            if (els[i].checked) { vals[vals.length] = els[i].value; }
        }
        return vals;
    }
};

AjaxSnippets.ValueExtractors.registerClass('AjaxSnippets.ValueExtractors');

AjaxSnippets._SnippetManager = function() {
    AjaxSnippets._SnippetManager.initializeBase(this);
    if (typeof (Prototype) === 'undefined') { alert('Prototype required - http://www.prototypejs.org/'); }
    this._callbacks = {};
    this._loadedScriptUrls = new Array();
    this._extractors = new AjaxSnippets.ValueExtractors();
};

AjaxSnippets._SnippetManager.prototype = {
    initialize: function() {
        AjaxSnippets._SnippetManager.callBaseMethod(this, 'initialize');
        var scripts = document.getElementsByTagName("script");
        for (var i = 0; i < scripts.length; i++) {
            if (scripts[i].src) {
                this._loadedScriptUrls[this._loadedScriptUrls.length] = scripts[i].src;
            }
        }
    },
    registerCallback: function(name, servicePath, serviceMethod, componentId, beforeCallback, callbackSuccess, callbackFailed, mappings) {
        var callbackObj = (componentId === null) ? window : $find(componentId);
        if (typeof (callbackObj) === 'undefined') { throw Error.argumentUndefined('componentId', 'Component not found'); }
        if (typeof callbackObj[beforeCallback] === 'undefined') { throw Error.argumentUndefined('beforeCallback', 'Undefined function on designated callback object'); }
        if (typeof callbackObj[callbackSuccess] === 'undefined') { throw Error.argumentUndefined('callbackSuccess', 'Undefined function on designated callback object'); }
        if (typeof callbackObj[callbackFailed] === 'undefined') { throw Error.argumentUndefined('callbackFailed', 'Undefined function on designated callback object'); }

        this._callbacks[name] = Function.createDelegate(this, function() {
            var i, callBackArgs = {}, before = callbackObj[beforeCallback];
            for (i = 0; i < mappings.length; i++) {
                callBackArgs[mappings[i].param] = this._extractors[mappings[i].func](mappings[i].id);
            }
            before();
            Sys.Net.WebServiceProxy.invoke(
                servicePath,
                serviceMethod,
                false,
                callBackArgs,
                Function.createDelegate(callbackObj, callbackObj[callbackSuccess]),
                Function.createDelegate(callbackObj, callbackObj[callbackFailed])
            );
        });
    },
    callback: function(name) {
        var callbackFunc = this._callbacks[name];
        if (typeof (callbackFunc) === 'undefined') {
            throw Error.argumentOutOfRange('name', name, 'SnippetManager: No callback was found with the given name');
        }
        else {
            callbackFunc();
        }
    },
    loadScript: function(url) {
        if (this._loadedScriptUrls.indexOf(url) == -1) {
            this._loadedScriptUrls[this._loadedScriptUrls.length] = url;
            var e = document.createElement("script");
            e.src = url;
            e.type = "text/javascript";
            document.getElementsByTagName("head")[0].appendChild(e);
        }
    },
    removeComponent: function(id){
        var c = $find(id);
        if(c) { Sys.Application.removeComponent(c); }
    },
    dispose: function() {
        for (var x in this._callbacks) {
            this._callbacks[x] = null;
        }
        AjaxSnippets._SnippetManager.callBaseMethod(this, 'dispose');
    }
};

AjaxSnippets._SnippetManager.registerClass('AjaxSnippets._SnippetManager', Sys.Component);

Sys.Application.add_init(function() {
    AjaxSnippets.SnippetManager = $create(AjaxSnippets._SnippetManager, { 'id': 'SnippetManager' });
});
if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }
